package com.cqjtu.eecs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cqjtu.eecs.entity.CourseRequirement;
import com.cqjtu.eecs.entity.Requirement;
import com.cqjtu.eecs.entity.dto.CourseBackGraduateDTO;
import com.cqjtu.eecs.entity.dto.CourseBackWeightDTO;
import com.cqjtu.eecs.entity.dto.CourseDTO;
import com.cqjtu.eecs.entity.dto.RequirementDTO;
import com.cqjtu.eecs.entity.vo.CourseBackGraduateVO;
import com.cqjtu.eecs.entity.vo.CourseRequirementVO;
import com.cqjtu.eecs.mapper.CourseRequirementMapper;
import com.cqjtu.eecs.mapper.RequirementMapper;
import com.cqjtu.eecs.service.ICourseRequirementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程-毕业要求 服务实现类
 * </p>
 *
 * @author    
 * @since 2022-03-07
 */
@Service
public class CourseRequirementServiceImpl extends ServiceImpl<CourseRequirementMapper, CourseRequirement> implements ICourseRequirementService {

    @Autowired
    private  CourseRequirementMapper courseRequirementMapper;

    @Autowired
    private RequirementMapper requirementMapper;

    @Override
    public CourseBackGraduateVO getCourseRequirementByPid(String pid) {

        CourseRequirementVO courseRequirementVO = new CourseRequirementVO();
        String sumRequirement= requirementMapper.selectCount(new QueryWrapper<Requirement>().eq("pid",pid).isNull("pre_requirement_id")).toString();
        courseRequirementVO.setSumRequirement(sumRequirement);

        courseRequirementVO.setRequirementDTOS(requirementMapper.getPreRequirementByPid(pid));

        courseRequirementVO.setCourseVOS(courseRequirementMapper.getCourseRequirement(pid));

        Integer graduateTopCount = Integer.parseInt(courseRequirementVO.getSumRequirement()); // 总数

        List<RequirementDTO> requirementDTOS = courseRequirementVO.getRequirementDTOS();
        Map<String, Integer> map = new HashMap<>();

        requirementDTOS.forEach(item->{
            map.put(item.getRequirement_code(), item.getPre_requirement_id().size());
        });

        List<CourseBackGraduateDTO> courseBackGraduateDTOS = new ArrayList<>();

//        System.out.println(courseRequirementVO);
        List<CourseDTO> courseVOS = courseRequirementVO.getCourseVOS();

        courseVOS.forEach(item->{
            Map<String, Float> courseGraduateMap = new HashMap<>();
            List<CourseBackWeightDTO> courseBackWeightDTOS = item.getCourseBackWeightDTOS();
             courseBackWeightDTOS.forEach(it->{
                 courseGraduateMap.put(it.getRequirement_code(), Float.parseFloat(it.getBackWeight()));
             });
//            System.out.println(courseGraduateMap.hashCode());
            courseBackGraduateDTOS.add(new CourseBackGraduateDTO(item.getCourse_name(), courseGraduateMap));
        });

        return new CourseBackGraduateVO(graduateTopCount,map,courseBackGraduateDTOS);
    }
}
